//
//  ActivityListViewController.m
//  UI-豆瓣项目APP
//
//  Created by dllo on 15/9/8.
//  Copyright (c) 2015年 孙鹏. All rights reserved.
//

#import "ActivityListViewController.h"
#import "ActivityDetailView.h"
#import "ActivityDetailViewController.h"
#import "MBProgressHUD.h"
#import "UserLandViewController.h"
#import "DateBaseSingleton.h"
@interface ActivityListViewController ()
@property(nonatomic,retain)MBProgressHUD *hud;
@end

@implementation ActivityListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
    self.allArray = [NSMutableArray array];
    self.navigationItem.title = @"活动";
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.activityTableView = [[UITableView alloc]initWithFrame:[[UIScreen mainScreen]bounds] style: UITableViewStylePlain];
    self.activityTableView.delegate =self;
    self.activityTableView.dataSource =self;
    [self.view addSubview:self.activityTableView];
    self.view.backgroundColor = [UIColor whiteColor];
    
    
#pragma mark -- 打开数据库
    [[DateBaseSingleton shareDateBase]openDB];
    
    
#pragma mark -- 查找
   
    NSMutableArray *array = [NSMutableArray array];
   array = [[DateBaseSingleton shareDateBase]selectActivity];
    
    self.allArray = array;
    
    
//    
//    NSString *document = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
//    
//    
//    //生成一个路径名
//    self.arrayPath = [document stringByAppendingPathComponent:@"activityList.plist"];
//    NSLog(@"%@", self.arrayPath);
    
//    
//    Reachability *r = [Reachability reachabilityWithHostName:@"www.apple.com"];
//    switch ([r currentReachabilityStatus]) {
//        case NotReachable:
//            NSLog(@"没有网络连接");
//          //反归档,取出数据
////            self.localArray = [NSKeyedUnarchiver unarchiveObjectWithFile:self.arrayPath];
////
////            [self.hud hide:YES];
//            // 没有网络连接
//            break;
//        case ReachableViaWWAN:
//            // 使用3G网络
//            NSLog(@"使用3G网络");
//            
//            break;
//        case ReachableViaWiFi:
//            // 使用WiFi网络
//            NSLog(@"使用WiFi网络");
//    
//            
//    
//            break;
//    }
    
#pragma mark -- 从本地取数据
    
    
    

//    
//    for (NSMutableDictionary *dic in self.localArray) {
//        
//        Activitylist *activity = [[Activitylist alloc] init];
//        
//        [activity setValuesForKeysWithDictionary:dic];
//        
//        [self.allArray addObject:activity];
//        [activity release];
//    }
//    
//    [self.activityTableView reloadData];
    
    
    
    
    
    
   
    
    [_activityTableView release];
    
    
    [self.hud hide:YES];
    [self addHeaderRefresh];
    self.isUpload = NO;

    
#pragma mark -- 从网络取
    
    
    [self getAFNetWorkingData];
    
    
    

   
    
    
    
    
    
    
}



-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    return 186;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *cellIndefider = @"cell";
    ActivityListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndefider];
    if (cell == nil) {
        cell = [[ActivityListTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndefider];
    }
    
    
    Activitylist *activity = [self.allArray objectAtIndex:indexPath.section];
   
    cell.activity = activity;
    
    return cell;
    
}



-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
   
    return self.allArray.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    
    
    
    return 1;
    
    
}






//点击cell触发的方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    ActivityDetailViewController *actV = [[ActivityDetailViewController alloc]init];
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    actV.indexPath = indexPath;
    [self.navigationController pushViewController:actV animated:YES];
    
    actV.activity = [self.allArray objectAtIndex:indexPath.section];

    
    
    
    
    
}


-(void)addHeaderRefresh
{
    //当下拉时候会触发该方法
    [self.activityTableView addHeaderWithCallback:^{
        
       
        
        [self getAFNetWorkingData];
        self.isUpload = NO;
        
        
        
    }];
    
//    [self.activityTableView headerBeginRefreshing];
}

-(void)addFooterRefresh
{
    //当上拉时候触发该方法
    [self.activityTableView addFooterWithCallback:^{
        
        NSLog(@"上拉");
        self.isUpload = YES;
        
    }];
}

-(void)getAFNetWorkingData{

[AppTools getDataWithFileURL:@"http://project.lanou3g.com/teacher/yihuiyun/lanouproject/activitylist.php" block:^(NSMutableDictionary *result) {
    
    
#pragma mark -- 删除整张表
    [[DateBaseSingleton shareDateBase]dropTable];
    
#pragma mark -- 创建表
    [[DateBaseSingleton shareDateBase]creatTable];
//    self.localArray = [NSMutableArray array];
    
    
    NSMutableArray *array = [result objectForKey:@"events"];
#pragma mark - 存入本地
    
    
    //判断是上拉还是下拉    下拉删除  上拉不删   设置一个属性在方法里  判断属性的值
    if (self.isUpload == NO) {
        
        [self.allArray removeAllObjects];
        
    }
    
    
   
    
    for (NSMutableDictionary *dic in array) {
        
        Activitylist *activity = [[Activitylist alloc] init];
        
        [activity setValuesForKeysWithDictionary:dic];
        
        [[DateBaseSingleton shareDateBase]insertActivity:activity];
        
        [self.allArray addObject:activity];
        [activity release];
        
    }
    
    

    
    
    

    self.hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [self.hud show:YES];
    self.hud.labelText = @"正在加载中...";
    
//    self.localArray = array;
//    
//    [self.localArray writeToFile:self.arrayPath atomically:YES];
    //归档操作
//    [NSKeyedArchiver archiveRootObject:self.localArray toFile:self.arrayPath];
    
    [self.activityTableView headerEndRefreshing];
    [self.activityTableView reloadData];
    
    
    
    [self.hud hide:YES];
    
    
    
    
}];


}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
